package com.lam.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.base.domain.tree.TreeNode;
import com.lam.common.base.domain.tree.TreeUtil;
import com.lam.common.base.page.TableData;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.components.easyexcel.export.DownloadUtil;
import com.lam.common.constant.CommonConstant;
import com.lam.common.constant.SystemConstant;
import com.lam.common.utils.JsonUtil;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.group.Add;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.model.ValidOperType;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.system.entity.SysDept;
import com.lam.system.param.SysDeptParam;
import com.lam.system.service.SysDeptService;

/**
 * 组织机构Controller
 * 
 * @author admin
 * @date 2022-10-18
 */
@Log(title = "组织机构")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/sys/dept")
public class SysDeptController extends BaseController {

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 查询组织机构列表
     */
    @Log(oper="列表查询")
    @DictAutoTransfer(deep = true)
    @PreAuthorize("@ss.hasPm('sys:dept:list')")
    @GetMapping("/list")
    public Result list(SysDeptParam param) {
    
        List<SysDept> list = sysDeptService.selectList(param);
        list = TreeUtil.buildTree(list);
        return Result.ok(new TableData(list, list.size()));
    }

    /**
     * 组织机构下拉树列表
     */
    @Log(oper="组织机构下拉树列表")
    @PreAuthorize("@ss.hasPm('sys:dept:list')")
    @GetMapping("/treeselect")
    public Result treeselect(SysDeptParam param) {
    
    	PageHelper.orderBy("parent_id, sort_no");
        List<SysDept> list = sysDeptService.selectList(param);
        List<TreeNode> treeNodeList = JsonUtil.toJavaList(list, TreeNode.class, "id", "parentId", "deptName=>title", "sortNo");
        return Result.ok(TreeUtil.buildTree(treeNodeList, CommonConstant.TOP_PARENT_ID));
    }

    /**
     * 导出组织机构列表
     */
    @PreAuthorize("@ss.hasPm('sys:dept:export')")
    @Log(businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Result export(SysDeptParam param) {
    
        withOrderBy();
        List<SysDept> list = sysDeptService.selectList(param);
        return Result.ok(DownloadUtil.exportExcel("组织机构导出", list, SysDept.class));
    }

    /**
     * 获取组织机构详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('sys:dept:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(sysDeptService.selectById(id));
    }

    /**
     * 新增组织机构
     */
    @PreAuthorize("@ss.hasPm('sys:dept:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody SysDept sysDept) {
    
        ValidateUtil.validUnique(sysDept, ValidOperType.ADD, SystemConstant.UNIQUE_DEPT_CODE, "机构编码已存在！");
        return toAjax(sysDeptService.insert(sysDept));
    }

    /**
     * 修改组织机构
     */
    @PreAuthorize("@ss.hasPm('sys:dept:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody SysDept sysDept) {
    
        ValidateUtil.validUnique(sysDept, ValidOperType.UPDATE, SystemConstant.UNIQUE_DEPT_CODE, "机构编码已存在！");
        return toAjax(sysDeptService.update(sysDept));
    }

    /**
     * 删除组织机构
     */
    @PreAuthorize("@ss.hasPm('sys:dept:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
        return toAjax(sysDeptService.deleteById(id));
    }

}
